package com.jiezuo.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jiezuo.auth.entity.Permission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface PermissionMapper extends BaseMapper<Permission> {

    /*@Select("SELECT " +
            " sr.role_name AS role_name, " +
            " sr.role_depart AS role_depart," +
            " sr.data_authority_level AS data_authority_level, " +
            " sp.resource_name AS resource_name," +
            " spa.type AS type," +
            " spa.api AS api," +
            " spa.interception_field AS interception_field  " +
            "FROM " +
            " sys_user_role sur" +
            " LEFT JOIN sys_role sr ON sr.id = sur.role_id AND sr.is_delete=0 " +
            " LEFT JOIN sys_role_permission srp ON srp.role_id = sur.role_id " +
            " LEFT JOIN sys_permission sp ON sp.id = srp.permission_id AND sp.is_delete=0 " +
            " LEFT JOIN sys_permission_api spa ON spa.permission_id = sp.id AND spa.is_delete=0 " +
            "WHERE " +
            " sur.user_id = #{user_id} " +
            " AND spa.is_data_authority = 1 " +
            " ORDER BY sr.data_authority_level DESC")*/
    @Select("SELECT " +
            " sr.code AS role_code, " +
            " sr.role_name AS role_name, " +
//            " sr.role_depart AS role_depart, " +
//            " sr.data_authority_level AS data_authority_level, " +
            " IF(srp.data_authority_level is not null,srp.role_depart,sr.role_depart) AS role_depart, " +
            " IF(srp.data_authority_level is not null,srp.data_authority_level,sr.data_authority_level) AS data_authority_level, "+
            " sp.resource_name AS resource_name, " +
            " spa.api AS api, " +
            " spaf.type AS type, " +
            " spaf.interception_field AS interception_field " +
            "FROM " +
            " sys_user_role sur" +
            " LEFT JOIN sys_role sr ON sr.id = sur.role_id AND sr.is_delete=0 " +
            " LEFT JOIN sys_role_permission srp ON srp.role_id = sur.role_id " +
            " LEFT JOIN sys_permission sp ON sp.id = srp.permission_id AND sp.is_delete=0 " +
            " LEFT JOIN sys_permission_api spa ON spa.permission_id = sp.id AND spa.is_delete=0 " +
            " LEFT JOIN sys_permission_api_field spaf ON spa.id = spaf.api_id AND spaf.is_delete=0 " +
            "WHERE 1=1" +
            " AND sur.user_id=#{user_id}" +
            " AND spa.is_data_authority=1 " +
            " ORDER BY sr.data_authority_level DESC")
    List<Map<String, Object>> findRoleAndPermissionByUserId(@Param("user_id") String user_id);

    List<String> findByUserId(@Param("user_id") String user_id);

    List<String> findButtonByUserId(@Param("user_id") String user_id);


    @Select("SELECT  " +
            " sr.role_name, " +
            " IF(srp.data_authority_level is not null and srp.data_authority_level !='',srp.role_depart,sr.role_depart) AS role_depart, " +
            " IF(srp.data_authority_level is not null and srp.data_authority_level !='',srp.data_authority_level,sr.data_authority_level) AS data_authority_level, " +
            " ( " +
            "  SELECT GROUP_CONCAT(su.real_name) FROM sys_user_role sur " +
            "  LEFT JOIN sys_user su ON su.id=sur.user_id " +
            "  WHERE sur.role_id=sr.id AND su.is_delete=0 " +
            " ) AS users " +
            "FROM sys_role_permission srp  " +
            "LEFT JOIN sys_role sr ON sr.id=srp.role_id " +
            "WHERE srp.permission_id=#{id} AND sr.is_delete=0")
    List<Map<String, Object>> findResourceDetails(@Param("id") String id);

}
